﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LogicaNegocio;

namespace UI
{
    public partial class FrmRecuperarContraseña : Form
    {
        public FrmRecuperarContraseña()
        {
            InitializeComponent();
        }

        #region Click

        private void btnSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRecuperar_Click(object sender, EventArgs e)
        {
            this.lblMensaje.Text = "";
            this.ptbLoading.Visible = true;
            this.btnRecuperar.Enabled = false;
            this.bgwRecuperar.RunWorkerAsync();
        }

        #endregion

        #region BackgroundWorker

        private void bgwRecuperar_DoWork(object sender, DoWorkEventArgs e)
        {
            string e_mail = this.txtE_Mail.Text;
            string id = this.txtUsuario.Text;
            UsuarioLN usuario = new UsuarioLN();
            try
            {
                this.bgwRecuperar.ReportProgress(1, "Validando...");
                usuario.RecuperarContraseña(e_mail, id);
                this.bgwRecuperar.ReportProgress(2, "Datos enviados.");
            }
            catch (Exception ex)
            {
                this.bgwRecuperar.ReportProgress(4, ex.Message);
            }
        }

        private void bgwRecuperar_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.lblMensaje.Text = e.UserState.ToString();
            if (e.ProgressPercentage == 2)
            {
                this.txtUsuario.Clear();
                this.txtUsuario_Leave(sender, e);
                this.txtE_Mail.Clear();
                this.txtE_Mail_Leave(sender, e);
            }
        }

        private void bgwRecuperar_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.ptbLoading.Visible = false;
            this.btnRecuperar.Enabled = true;
        }

        #endregion

        #region TextBox

        private void txtUsuario_Enter(object sender, EventArgs e)
        {
            if (this.txtUsuario.Text == "Usuario/Cedula")
            {
                this.txtUsuario.Text = "";
                this.txtUsuario.ForeColor = SystemColors.Desktop;
            }
        }

        private void txtUsuario_Leave(object sender, EventArgs e)
        {
            if (this.txtUsuario.Text == "")
            {
                this.txtUsuario.Text = "Usuario/Cedula";
                this.txtUsuario.ForeColor = SystemColors.GrayText;
            }
        }

        private void txtE_Mail_Enter(object sender, EventArgs e)
        {
            if (this.txtE_Mail.Text == "E_Mail")
            {
                this.txtE_Mail.Text = "";
                this.txtE_Mail.ForeColor = SystemColors.Desktop;
            }
        }

        private void txtE_Mail_Leave(object sender, EventArgs e)
        {
            if (this.txtE_Mail.Text == "")
            {
                this.txtE_Mail.Text = "E_Mail";
                this.txtE_Mail.ForeColor = SystemColors.GrayText;
            }
        }

        #endregion

    }
}
